import time
import os
from common.save_records import SaveFile
from pages.target import Target


def spider_target(excel_file_name, product_url):
    print("Task:%s..is staring" % excel_file_name)
    create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))
    excel_file_full_path_name = os.path.join("D:\\pycode\\JT_spider\\log", 'grandinroad_%s_%s.xlsx' % (excel_file_name,create_time))
    produce_img_dir_path = "D:\\pycode\\JT_spider\\log\\img"

    save_files = SaveFile(excel_file_full_path_name, produce_img_dir_path)
    excel_file, excel_file_format, file_object = save_files.create_excel()
    if not os.path.exists(produce_img_dir_path):
        os.mkdir(produce_img_dir_path)

    spider = Target(product_url)

    row_num = 1
    for info in spider.get_all_items():
        img_path = save_files.download_img(info[0])
        excel_file.set_row(row_num, 250)
        excel_file.insert_image(row_num, 0, img_path)
        for j in range(1, len(info)):
            excel_file.write(row_num, j, info[j], excel_file_format)
        row_num += 1
    file_object.close()


if __name__ == "__main__":
    start_time = time.time()
    info_dic = dict()
    excel_file_name1 = "joann"
    web_url1 = "https://www.target.com/c/outdoor-christmas-decorations/inflatable-holiday-decorations/-/N-5xt2tZgxyr9?type=products&sortBy=bestselling"
    web_url2 = "https://www.target.com/c/christmas-lights/-/N-5xt2w?type=products&sortBy=bestselling"
    web_url3 = "https://www.target.com/c/indoor-christmas-decorations/-/N-5xt2x?type=products&sortBy=bestselling"

    spider_target(product_url=web_url3, excel_file_name=excel_file_name1)
    print("Spend time: %s" % (time.time() - start_time))



    # import requests

    # headers = {
    #     # ":authority":  "www.joann.com",
    #     # ":method":  "GET",
    #     # ":path":  "/bloom-room-holiday-christmas-mini-poinsettia-bush-white-8in/17699406.html",
    #     # ":scheme":  "https",
    #     "accept":  "text/html,application/xhtml+xml,application/xml;q=0.9",
    #     "accept-encoding":  "gzip, deflate, br",
    #     "accept-language":  "zh-CN,zh;q=0.9",
    #     "cache-control":  "max-age=0"
    # }
    # rep = requests.get("https://target.scene7.com/is/image/Target/GUEST_64f9f8ae-4527-44da-b803-2978efe3150b", headers=headers)

    # with open("rep.jpg", "wb") as f:
    #     f.write(rep.content)